<?php

/*
get page indexes html
*/
function html_pager($url,$total_page,$page = 1)
{
	$page_span = 4;
	$pager = '';
	if ($page>2)
		$pager .= ' <a href="'.$url.'&page=1">'.LANG_PAGER_FIRST.'</a> ';
	if ($page>1)
		$pager.= ' <a href="'.$url.'&page='.($page-1).'">'.LANG_PAGER_PREV.'</a> ';
	
	if ($page>$page_span)
		$pager.= '...';
	
	for($i=$page-$page_span+1;$i<$page+$page_span;$i++)
	{
		if ($i<=0 || $i>$total_page) continue;
		if ($page == $i)
			$pager.= " [{$i}] ";
		else
			$pager.= ' <a href="'.$url.'&page='.$i.'">'.$i.'</a> ';
	}
	
	if ($total_page-$page>$page_span)
		$pager.= '...';
	
	
	if ($page<$total_page)
		$pager.= ' <a href="'.$url.'&page='.($page+1).'">'.LANG_PAGER_NEXT.'</a> ';
	if ($page+1<$total_page)
		$pager .= ' <a href="'.$url.'&page='.$total_page.'">'.LANG_PAGER_LAST.'</a> ';
	return $pager;
}

function lz_page($obj,$cond = array(),$per_page)
{
	global $view_data;

	$cond['select'] = 'count(*)';
	$total = $obj->get_list( $cond );
	$total = $total[0]['count(*)'];
	$page = intval($_GET['page']);
	$per_page <= 0 && $per_page = 20;
	!$page && $page=1;
	$total_page = ceil($total/$per_page);
	!$total_page && $total_page=1;
	$page>$total_page && $page = $total_page;
	$cond['select'] = '*';
	$cond['from'] = ($page-1)*$per_page;
	$cond['total'] = $per_page;
	$pager = LANG_PAGER_TOTAL.$total.LANG_PAGER_ITEMS.$total_page.LANG_PAGER_PAGE.'<br />';
	$url = $_SERVER['REQUEST_URI'];
	$url = preg_replace('/&?page=[0-9]{1,}/','',$url);
	$pager.=html_pager($url,$total_page,$page);
	$view_data['pager'] = $pager;
	return $obj->get_list( $cond );
}


/*
make empty folder only
*/
function lz_mkdir($path)
{
	$arr = explode('/',$path);
	for($i=0;$i<=count($arr);$i++)
	{
		$p.= $arr[$i].'/';
		if (!is_dir($p)) mkdir($p);
	}
	return $p;
}

/*
make category folder and copy index.php to that folder
*/
function lz_make_category($path,$data)
{
	$s = file_get_contents(LZ_BASEPATH.'library/templates/item_list.tpl');
	foreach($data as $key=>$val)
	{
		$s = str_replace('{'.$key.'}',$val,$s);
	}
	$path = lz_mkdir(LZ_TOPPATH.$path);
	if (is_dir($path))
		return file_put_contents($path.'index.php',$s);
	else
		return false;
}

/*
make blog item folder and copy index.php to that folder
*/
function lz_make_item($path,$data)
{
	$s = file_get_contents(LZ_BASEPATH.'library/templates/item.tpl');
	foreach($data as $key=>$val)
	{
		$s = str_replace('{'.$key.'}',$val,$s);
	}
	$path = lz_mkdir(LZ_TOPPATH.$path);
	if (is_dir($path))
		return file_put_contents($path.'index.php',$s);
	else
		return false;
}

/*
check url name
*/
function ck_url_name($s)
{
	if (!preg_match('/^[0-9a-zA-Z_]{1,}$/',$s)) return false;
	return $s;
}


/*
get blog item url path
*/
function get_item_path($data)
{
	if (!is_array($data) && @intval($data) > 0)	$data = array('item_id'=>$data);
	if (!$data['url_name'] && $data['item_id'])
	{
		include_once('model/item.php');
		$item = new LZ_Item;
		$_arr = $item->get_one($data['item_id']);
		$data['url_name'] = $_arr['url_name'];
		$data['publish_time'] = $_arr['publish_time'];
	}
	if ($data['url_name'] && $data['publish_time'])
	{
		return date('Y',$data['publish_time'])
			.'/'.date('m',$data['publish_time']).'/'.$data['url_name'].'/';
	}
	else return '';
}

/*
mv item folder
*/
function lz_mv($p1,$p2)
{
	$p1 = LZ_TOPPATH.$p1;
	$p2 = LZ_TOPPATH.lz_mkdir($p2);
	rename($p1,$p2);
}

/*
delete item folder
*/
function lz_delete($path)
{
	unlink(LZ_TOPPATH.$path.'/index.html');
	unlink(LZ_TOPPATH.$path.'/index.php');
	return rmdir(LZ_TOPPATH.$path);
}

/*
update 
*/
function lz_update($p = '')
{
	$f = LZ_TOPPATH.$p.'index.html';
	if (file_exists($f)) 
		return unlink($f);
	//else
	//	die('update error! '.$f);
}

/*
send download header and output filedata
*/
function download_file($filepath, $filename = '')
{
	if(!$filename) $filename = basename($filepath);
	if(is_ie()) $filename = rawurlencode($filename);
	$filetype = strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
	$filesize = sprintf("%u", filesize($filepath));
	if(@ob_get_length() !== false) @ob_end_clean();
	header('Pragma: public');
	header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
	header('Cache-Control: no-store, no-cache, must-revalidate');
	header('Cache-Control: pre-check=0, post-check=0, max-age=0');
	header('Content-Transfer-Encoding: binary');
	header('Content-Encoding: none');
	header('Content-type: '.$filetype);
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	header('Content-length: '.$filesize);
	readfile($filepath);
	exit;
}

?>